package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Admin;
import cn.edu.ujn.shi.dao.AdminDto;
import cn.edu.ujn.shi.service.IAdminService;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    IAdminService iAdminService;

    /**
     * 返回管理员列表页面
     *
     * @param request
     * @param response
     */
    @RequestMapping("/AListAdminServlet")
    public void AListAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            String userName = request.getParameter("username");
            // 查找管理员列表
            List<Admin> list = iAdminService.getByUserName(userName);

            PrintWriter out = response.getWriter();
            //引用jsp文件
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header-admin.jsp");
            headerDispatcher.include(request, response);
            out.println("<center><img src='images/权限.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>管理员账号管理</b></font></center>");
            out.println("<hr/>");


            out.println("<br/>");
            out.println("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='AddAdmin'><img src='images/add.png' width='100px'/></a>");
            out.println("<center>");
            out.println("    <form action='AListAdminServlet' method='get'>\n" +
                    "        <label for='username'>按用户名搜索：</label>\n" +
                    "        <input type='text' id='username' name='username'>\n" +
                    "        <button type='submit'>搜索</button>\n" +
                    "    </form>");
            out.println("<table border=1 width=90% >");
            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td width='200px'><b>用户名</b></td ><td width='200px'><b>密码</b></td><td width='200px'><b>操作</b></td>");
            out.println("</tr>");

            for (Admin u : list) {
                out.println("<tr>");
                out.println("<td width='200px'>");
                out.println(u.getUsername());
                out.println("</td>");
                out.println("<td width='200px'>");
                out.println(u.getUserpass());
                out.println("</td>");
                out.println("<td width='200px'>");
                out.println("<a href='AddAdmin?username=" + u.getUsername() + "&userpass=" + u.getUserpass() + "'>修改管理员</a>");
                out.println("<a href='DeleteAdminServlet?username=" + u.getUsername() + "'>删除管理员</a>");

                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table></center>");

            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除管理员请求
     *
     * @param request
     * @param response
     */
    @RequestMapping("/DeleteAdminServlet")
    public void deleteAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            String username =request.getParameter("username");
            iAdminService.delete(username);

            response.sendRedirect("AListAdminServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增管理员
     *
     * @param request
     * @param response
     */
    @RequestMapping("/InsertAdminServlet")
    public void insertAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();

            request.setCharacterEncoding("utf-8");

            // 从请求中获取入参
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");

            if (username.equalsIgnoreCase("") || userpass.equalsIgnoreCase("")) {
                out.println("<script language = javascript>alert('WARNING')");
                out.println("window.history.go(-1)</script>");
            } else {
                // 判断重复
                List<Admin> list = iAdminService.getByUserName(username);
                try {
                    if (!list.isEmpty()) {
                        out.println("<script language = javascript>alert('用户重复!')");
                        out.println("window.history.go(-1)</script>");
                    } else {

                        // 没有重复新增
                        iAdminService.insert(new Admin(username, userpass));
                        out.println("<script language = javascript>alert('SUCCESS!')");
                        response.sendRedirect("AListAdminServlet");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/UpdateAdminServlet")
    public void UpdateAdminServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();

            request.setCharacterEncoding("utf-8");

            // 从请求中获取入参
            String username = request.getParameter("username");
            String userpass = request.getParameter("userpass");
            String username_old = request.getParameter("username_old");

            if (username.equalsIgnoreCase("") || userpass.equalsIgnoreCase("")) {
                out.println("<script language = javascript>alert('WARNING')");
                out.println("window.history.go(-1)</script>");
            } else {
                // 判断重复
                List<Admin> list = iAdminService.getByUserName(username);
                try {
                    if (!username.equals(username_old) && !list.isEmpty()) {
                        out.println("<script language = javascript>alert('用户重复!')");
                        out.println("window.history.go(-1)</script>");
                    } else {

                        // 没有重复新增
                        iAdminService.updateAdmin(new AdminDto(username, userpass, username_old));
                        out.println("<script language = javascript>alert('SUCCESS!')");
                        response.sendRedirect("AListAdminServlet");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}